using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class MainMenu : MonoBehaviour
{
    [Header("State")]
    public List<GameObject> activity;

    [Header("Color")]
    public Color colorSelect;

    [Header("Requirement")]
    public Transform canvas;

    public Animation controllerAnimation;

    public GameObject creatureCollider;

    public Animation eggAnimation;

    public GameObject emptyCreature;

    public GameObject plug;

    public Scrollbar scrollbar;

    public Transform creditText;

    [Header("Window")]
    public GameObject windowGame;

    public GameObject windowMain;

    public GameObject windowLoading;

    private bool block;

    private Transform choosen;

    private GameObject windowOld;

    private SceneName sceneName;

    private const int control = 0;

    private void Start()
    {
        //Application.targetFrameRate = 30;
        windowOld = windowMain;
        Info.emptyTransform = new GameObject("Empty").transform;
        Info.path = Application.persistentDataPath + "/";
        LoadCreatures();
        if (!Library.plug)
        {
            UnityEngine.Object.Instantiate(plug);
        }
        Library.music.Play(SceneName.Menu);
        if (Progress.id == 0)
        {
            StartNewGame();
        }
    }

    private void Update()
    {
        if (block)
        {
            return;
        }
        if (Input.GetMouseButtonDown(0))
        {
            Catch();
        }
        else if (Input.GetMouseButtonUp(0))
        {
            Transform transform = choosen;
            if (Catch() && choosen == transform)
            {
                if (choosen.name == "Jajo")
                {
                    StartNewGame();
                }
                else
                {
                    LoadGame();
                }
            }
        }
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (activity.Count == 0)
            {
                Quit();
            }
            else
            {
                Return();
            }
        }
    }

    private void LoadCreature()
    {
        Progress.Load();
        Vector2 vector;
        do
        {
            vector = new Vector2(UnityEngine.Random.Range(-10f, 8f), UnityEngine.Random.Range(-3f, 8f));
        }
        while ((bool)Physics2D.OverlapPoint(vector));
        Creature component = UnityEngine.Object.Instantiate(emptyCreature).GetComponent<Creature>();
        CreatureLoader.Fill(component, false);
        Enemy enemy = component as Enemy;
        Texture2D texture2D = new Texture2D(512, Info.textureHeight);
        texture2D.LoadImage(Convert.FromBase64String(PlayerPrefs.GetString(Progress.TexturePath())));
        texture2D.Apply();
        Renderer[] componentsInChildren = component.GetComponentsInChildren<Renderer>();
        Material material = new Material(componentsInChildren[0].material);
        material.mainTexture = texture2D;
        Material material2 = material;
        Renderer[] array = componentsInChildren;
        foreach (Renderer renderer in array)
        {
            if (renderer.material.name == "Cialo (Instance)")
            {
                renderer.material = material2;
            }
        }
        GameObject gameObject = UnityEngine.Object.Instantiate(creatureCollider, component.model);
        gameObject.name = Progress.id.ToString();
        component.root.localScale *= 0.75f;
        component.speed = 2f;
        enemy.Begin(vector);
    }

    private void LoadCreatures()
    {
        Progress.id = -1;
        while (Progress.HasNextSave())
        {
            LoadCreature();
        }
    }

    private void StartNewGame()
    {
        block = true;
        Progress.Load();
        Progress.points = 40;
        Progress.SaveColors(new Color32[2]
        {
            Color.white,
            Color.black
        });
        controllerAnimation.Play();
        eggAnimation.Play("birth");
        sceneName = SceneName.BodyShapeEditor;
        Invoke("Play", 2f);
    }

    private void LoadGame()
    {
        Progress.id = int.Parse(choosen.name);
        Progress.Load();
        sceneName = SceneName.CreatureEditor;
        WindowLoading();
        Play();
    }

    private void Play()
    {
        Scene.Load(sceneName);
    }

    public int OptionChange(byte id)
    {
        string @string = PlayerPrefs.GetString("Options");
        char @char = ((@string[id] != '0') ? '0' : '1');
        return OptionChange(id, @char);
    }

    public int OptionChange(byte id, char @char)
    {
        string @string = PlayerPrefs.GetString("Options");
        @string = @string.ReplaceAt(id, @char);
        PlayerPrefs.SetString("Options", @string);
        return @char - 48;
    }

    public int OptionChange(byte id, int valueNew)
    {
        char @char = (char)(valueNew + 48);
        return OptionChange(id, @char);
    }

    public int OptionGet(byte id)
    {
        return PlayerPrefs.GetString("Options")[id] - 48;
    }

    public void WindowLoading()
    {
        WindowShow(windowLoading);
    }

    public void WindowShow(GameObject window)
    {
        window.SetActive(true);
        if ((bool)windowOld)
        {
            windowOld.SetActive(false);
            if (!activity.Contains(window))
            {
                activity.Add(windowOld);
            }
        }
        windowOld = window;
        block = true;
    }

    private void WindowShow(int id)
    {
        WindowShow(canvas.GetChild(id).gameObject);
    }

    private void WindowShow(string name)
    {
        WindowShow(canvas.Find(name).gameObject);
    }

    public void WindowThanks()
    {
        Return();
        WindowShow("Thanks");
    }

    public void Return()
    {
        int count = activity.Count;
        if (count != 0)
        {
            GameObject gameObject = activity[count - 1];
            WindowShow(gameObject);
            activity.Remove(gameObject);
            block = false;
        }
    }

    private bool Catch()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;
        if (Physics.Raycast(ray, out hitInfo))
        {
            choosen = hitInfo.transform;
            return true;
        }
        choosen = null;
        return false;
    }

    public void Quit()
    {
        Application.Quit();
    }

    public void Hide()
    {
        GameObject.Find("Canvas").transform.Find("PrivacyPanel").gameObject.SetActive(true);
    }

    public void More()
    {
    }

    public void MoveText()
    {
        creditText.localPosition = new Vector2(0f, scrollbar.value * 1200f + 450f);
    }
}
